package com.happycat.configuration;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
	
	@Bean
    public Docket createRestApi() {
		ParameterBuilder ticketPar = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<Parameter>();
		ticketPar.name("token").description("登录校验")//name表示名称，description表示描述
		.modelRef(new ModelRef("string")).parameterType("header")
		.required(false)
                .defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjIwMjAwNzI3ODUwMDAwMDAsInVzZXJOYW1lIjoiMTM1Mzk0NDc1MjIiLCJyb2xlcyI6W10sInN1YiI6IjEzNTM5NDQ3NTIyIiwiaWF0IjoxNTk5NzA3OTg4LCJhdWQiOiJoYXBweWNhdCIsImV4cCI6MTYwMjI5OTk0MCwibmJmIjoxNTk5NzA3OTg4fQ.vZqHUgi5GTjzfjr3m2KhmT4lZAymb5yuGljghsYPq-U")
//                .defaultValue("Bearer ")
                .build();//required表示是否必填，defaultvalue表示默认值
		pars.add(ticketPar.build());//添加完此处一定要把下边的带***的也加上否则不生效

		return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //这里采用包含注解的方式来确定要显示的接口
                //.apis(RequestHandlerSelectors.basePackage("com.stylefeng.guns.modular.system.controller")) //这里采用包扫描的方式来确定要显示的接口
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);//************把消息头添加
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("奇乐猫客户端-api")
                .description("简单优雅的restfun风格")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/static/**").addResourceLocations(
//                "classpath:/static/");
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
//    }
}
